package com.kancy.kie.server.client.service;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * RuleParamsBuilder
 * <p>
 *
 * @author: kancy
 * @date: 2020/3/14 16:27
 **/

public class RuleParamsBuilder {

    private RuleParamsBuilder() {
    }

    private String containerId;

    private String sessionName;

    private Map<String, Object> insertMap = new LinkedHashMap<>();

    private Map<String, Object> globalMap = new LinkedHashMap<>();

    private AtomicInteger index = new AtomicInteger(0);

    /**
     * 插入全局对象到工作内存
     * @param identifier 对象身份id
     * @param object 对象数据
     * @return
     */
    public RuleParamsBuilder setGlobal(String identifier, Object object){
        globalMap.put(identifier, object);
        return this;
    }

    /**
     * 插入对象到工作内存
     * @param identifier 对象身份id
     * @param object 对象数据
     * @return
     */
    public RuleParamsBuilder insert(String identifier, Object object){
        insertMap.put(identifier, object);
        return this;
    }

    /**
     * 插入对象到工作内存
     * @param object 对象数据
     * @return
     */
    public RuleParamsBuilder insert(Object object){
        insertMap.put(String.format("%s_%s", object.getClass().getSimpleName(), index.getAndIncrement()), object);
        return this;
    }

    public RuleParams build(){
        RuleParams ruleParams = new RuleParams();
        ruleParams.setContainerId(containerId);
        ruleParams.setSessionName(sessionName);
        ruleParams.setInsertMap(insertMap);
        ruleParams.setGlobalMap(globalMap);
        return ruleParams;
    }

    public static RuleParamsBuilder create(String containerId){
        return new RuleParamsBuilder().setContainerId(containerId);
    }

    public static RuleParamsBuilder create(String containerId, String sessionName){
        return create(containerId).setSessionName(sessionName);
    }

    private RuleParamsBuilder setContainerId(String containerId) {
        this.containerId = containerId;
        return this;
    }

    private RuleParamsBuilder setSessionName(String sessionName) {
        this.sessionName = sessionName;
        return this;
    }
}
